package com.feicheng.persistence.service.impl;

import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.message.SystemMessage;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Role;
import com.feicheng.persistence.entity.User;
import com.feicheng.persistence.mapper.RoleMapper;
import com.feicheng.persistence.service.IRoleService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangzhaoyu
 * @since 2023-08-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

	@Autowired
	SystemMessage systemMessage;
	
	@Autowired
	HttpSession session;

	@Override
	public ResultInfo selectRoles(String name, Boolean aFlg, Integer currentPage, Integer pageSize) {
		QueryWrapper<Role> roleQueryWapper = new QueryWrapper<Role>();

        if (StringUtils.hasLength(name)) {
            roleQueryWapper.like(Constant.ENTITY.ROLE.NAME, name);
        }

        if (aFlg != null) {
            roleQueryWapper.eq(Constant.ENTITY.ROLE.A_FLG, aFlg);
        }

        IPage<Role> pageInfo = new Page<Role>(currentPage, pageSize);
        return ResultInfo.success(this.page(pageInfo, roleQueryWapper));
	
	}

	@Override
	public ResultInfo allRoles(Boolean aFlg) {
		QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<Role>();
		roleQueryWrapper.eq(Constant.ENTITY.ROLE.A_FLG, aFlg);
		return ResultInfo.success(this.list(roleQueryWrapper));
	}

	@Override
	public ResultInfo getRoleById(String roleCode) {
		if(!StringUtils.hasLength(roleCode)) {
			throw new SystemException(systemMessage.getE04());
		}
		
		Role role = this.getById(roleCode);
		
		if (null == role) {
			throw new SystemException(systemMessage.getE07());
		}
		
		JSONObject result = new JSONObject();
		result.put(Constant.RESULT.ROLE.ROLE, role);
		return ResultInfo.success(result);
	}

	@Override
	public ResultInfo addRole(Role role) {
		if (!StringUtils.hasLength(role.getRoleCode())) {
			throw new SystemException(systemMessage.getE05());
		}

		JSONObject current = (JSONObject) session.getAttribute(Constant.RESULT.CURRENT);
		int currentUserId = 0;
		if (current != null) {
			User currentUser = (User) current.get(Constant.RESULT.USER.USER);
            currentUserId = currentUser.getUserId();
		}
		
		this.save(role);
		return ResultInfo.success();
	}

	@Override
	public ResultInfo updateRole(Role role) {
		if (!StringUtils.hasLength(role.getRoleCode())) {
            throw new SystemException(systemMessage.getE04());
        }

        this.updateById(role);

        return ResultInfo.success();
	}

	@Override
	public ResultInfo deleteRole(String roleCode) {
		
				
		if (!StringUtils.hasLength(roleCode)) {
            throw new SystemException(systemMessage.getE04());
        }
		QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();
		queryWrapper.eq("role_code", roleCode);
		Long roleNumber = this.count(queryWrapper);
		if (roleNumber<=0) {
			throw new SystemException("角色信息不存在");
		}
		this.removeById(roleCode);
		return ResultInfo.success();
	}

}
